package com.yzt.zhuju.activity

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.fragment.app.FragmentActivity
import com.alibaba.android.arouter.launcher.ARouter
import com.yzt.p_base.utils.ToastUtil
import com.yzt.zhuju.databinding.BvBaseActivityBinding
//import com.nomichat.basemodule.databinding.BvBaseActivityBinding
import com.yzt.zhuju.widget.MCTitlebar
import com.yzt.zhuju.widget.ProgressDialog

/**
 * @Desc base
 * @Author macjay
 * @Time 2022/12/15 16:27
 */
abstract class BaseActivity : FragmentActivity() {

    private lateinit var viewBinding: BvBaseActivityBinding

    private var mProgressDialog: ProgressDialog? = null

    val mHandler: Handler by lazy { Handler(Looper.getMainLooper()) }

    lateinit var titleBar: MCTitlebar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        viewBinding = BvBaseActivityBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)
        initView()
    }

    abstract fun getContent(): View

    private fun initView() {
        titleBar = viewBinding.baseTitlebar
        viewBinding.baseFlContent.addView(getContent())
        if (hideTitleBar()) {
            viewBinding.baseTitlebar.visibility = View.GONE
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN) {
            val view = currentFocus
            if (isShouldHideKeyboard(view, ev) && shouldHide()) {
                view?.windowToken?.let {
                    val im = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    im.hideSoftInputFromWindow(it, InputMethodManager.HIDE_NOT_ALWAYS)
                }
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    open fun shouldHide(): Boolean {
        return true
    }

    private fun isShouldHideKeyboard(v: View?, event: MotionEvent): Boolean {
        if (v != null && v is EditText) {
            val l = intArrayOf(0, 0)
            v.getLocationInWindow(l)
            val left = l[0]
            val top = l[1]
            val bottom = top + v.getHeight()
            val right = left + v.getWidth()
            return !(event.x > left && event.x < right && event.y > top && event.y < bottom)
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false
    }

    /**
     * 显示进度
     */
    open fun showMProgress() {
        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog(this)
        }
        mProgressDialog!!.setShowContent("")
        if (!mProgressDialog!!.isShowing) {
            mProgressDialog!!.show()
        }
    }

    /**
     * 显示进度
     */
    open fun showMProgress(msg: String?) {
        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog(this)
            mProgressDialog!!.setCancelable(false)
        }
        if (!mProgressDialog!!.isShowing) {
            mProgressDialog!!.setShowContent(msg)
            mProgressDialog!!.show()
        }
    }

    open fun showToast(msg:String?){
        ToastUtil.showMessage(msg)
    }

    open fun hideTitleBar(): Boolean {
        return false
    }
}